import { observable, toJS } from 'mobx'
import { sys } from "@/utils/sys";

//全部唯一的数据字典
export let GlobalDictList = []; //全局的字段存放
export const setGlobalDictList = (list) => {
  GlobalDictList = list
}

export let IP = `http://192.180.3.199:9083/hospital-service`
export const setIP = (ip) => {
  IP = `http://${ip}:9083/hospital-service`
}

//定义全局的变量
const modelStore = observable({
  // IP
  // IP: `http://192.180.3.199:9083/hospital-service`,
  prefix: "yrd_hospital_",
  //全局token
  token: "",
  //全局token全部信息
  tokenData: {},
  //全局字典信息
  dictList: [],
  //全局用户信息
  userInfoData: {},
  //全局权限信息(包括menu和role)
  userRoleData: {},
  //全局权限信息
  roleData: {},
  //全局菜单信息
  menuData: [
    {
      component: "/System",
      name: "系统管理",
      path: "/System",
    }
  ],
  //获取token
  GetTokenByStore() {
    const token_data = sys.strToJson(sys.getItem(modelStore.prefix + "token_data"));
    console.log(token_data, "token_data")
    if (!sys.is_empty(token_data)) {
      modelStore.token = token_data.token
    } else {
      modelStore.token = ""
    }
    return modelStore.token
  },
  GetMenuLocal() {
    const menu_data = sys.strToJson(sys.getItem(modelStore.prefix + "menu_data"))
    modelStore.menuData = !sys.is_empty(menu_data) ? menu_data : [{
      component: "/System",
      name: "系统管理",
      path: "/System",
    }]
    console.log("===>menu_data===>", toJS(modelStore.menuData));
  },
  GetUserInfo() {
    const user_data = sys.strToJson(sys.getItem(modelStore.prefix + "user_data"))
    console.log("===>user_data===>", user_data);
    modelStore.userInfoData = user_data
  },
  //删除所有的静态缓存
  DelAllStore() {
    sys.removeItem(modelStore.prefix + "token_data")
    sys.removeItem(modelStore.prefix + "user_role_data")

  },
  //设置全部的token对象
  SetTokenData(data) {
    let newData = {
      data,
      "token": "Bearer " + data
    }
    modelStore.tokenData = newData
    modelStore.token = newData.token
    sys.setItem(modelStore.prefix + "token_data", sys.jsonToStr(newData)); //存放token
  },
  //设置菜单
  SetMenu(data) {
    modelStore.menuData = data
    sys.setItem(modelStore.prefix + "menu_data", sys.jsonToStr(data)); //存放token
  },
  //设置字典
  SetDict(data) {
    setGlobalDictList(data);
    sys.setItem(modelStore.prefix + "dict_data", sys.jsonToStr(data)); //存放token
  },
  //设置登录人的信息
  SetUserInfo(data) {
    modelStore.userInfoData = data
    sys.setItem(modelStore.prefix + "user_data", sys.jsonToStr(data)); //存放token
  },
  //设置全局的用户登录信息
  SetUserRoleData(data) {
    modelStore.userRoleData = data
    modelStore.roleData = data.role
    modelStore.menuData = data.menu
    sys.setItem(modelStore.prefix + "user_role_data", sys.jsonToStr(data)); //存放token
  },
  //数据字典信息
  dictData: []
})

export default modelStore



